package com.example.twoscroolview.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup

class FlowLayout : ViewGroup {
    lateinit  var childView: View
    lateinit var  lineViews: ArrayList<View>

    //item间距
    var lineVerSpec = 20f
    var lineHorSpec = 20f

    lateinit var allLines : MutableList<List<View>>//
    lateinit var lineHeights :MutableList<Float>//行高
    constructor(context: Context) : super(context) {
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )
    init {

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        allLines = mutableListOf<List<View>>()
        lineHeights = mutableListOf<Float>()
        //父view 给flowlayout的宽高
        val selfHeight = MeasureSpec.getSize(heightMeasureSpec)//viewGroup的宽
        val selfWidth = MeasureSpec.getSize(widthMeasureSpec)
        val paddingLeft = paddingLeft
        val paddingRight = paddingRight
        val paddingTop = paddingTop
        val paddingBottom = paddingBottom

        //保存一行的所有数据
         lineViews = arrayListOf<View>()//一行中的所有view
        var lineHeight = 0f//一行的高
        var lineWidth = 0f//记录这行已经使用宽度
        var parentNeedWidth = 0f//measure过程中，子view要求父viewgroup的宽
        var parentNeedHeight = 0f//measure过程中，子view要求父viewgroup的高
        for (i in 0 until childCount) {
             childView = getChildAt(i)
            val layoutParams = childView.layoutParams
            val childHeightMeasureSpec = getChildMeasureSpec(
                heightMeasureSpec,
                paddingTop + paddingBottom,
                layoutParams.height
            )
            val childWidthMeasureSpec = getChildMeasureSpec(
                widthMeasureSpec,
                paddingLeft + paddingRight,
                layoutParams.width
            )
            //测量子view
            childView.measure(childHeightMeasureSpec, childWidthMeasureSpec)

            //子view测量的宽高     //获取子view宽高
           val childWidth = childView.measuredWidth
            val childHeight= childView.measuredHeight

            //超过就换行
            if (childWidth + lineWidth + lineHorSpec > selfWidth) {
                allLines.add(lineViews)
                lineHeights.add(lineHeight)
                Log.e("mytag", "--allLines==" + allLines.size)
                //换行需要判断当前行需要的宽高
                parentNeedWidth = Math.max(lineWidth, widthMeasureSpec.toFloat() + lineHorSpec)
                parentNeedHeight = (parentNeedHeight + lineHeight + lineVerSpec)

                lineHeight = 0f
                lineWidth = 0f
                lineViews = arrayListOf<View>()

            }

            lineViews.add(childView)
            lineWidth = (lineWidth + childWidth + lineHorSpec)//当前行宽度累加
            lineHeight = Math.max(lineHeight, childHeight.toFloat())//当前行高和最高的子view一样
            if(i == childCount-1){
                allLines.add(lineViews)
                lineHeights.add(lineHeight)
                parentNeedHeight = (lineHeight + childHeight + lineVerSpec)
                parentNeedWidth = Math.max(lineWidth, widthMeasureSpec.toFloat() + lineHorSpec)

            }
        }
        //根据子view的度量结果，重新度量自己viewGroup,也要根据他父亲给他提供的宽高来度量
        val widthsSecMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightSpecMode = MeasureSpec.getMode(heightMeasureSpec)

        //如果父亲viewGroup是确切宽高，使用父亲viewGroup宽高，否则用子view计算的宽高
        setMeasuredDimension(
            if (widthsSecMode == MeasureSpec.EXACTLY) {
                selfWidth
            } else {
                parentNeedWidth.toInt()
            },

            if (heightSpecMode == MeasureSpec.EXACTLY) {
                selfHeight
            } else {
                parentNeedHeight.toInt()
            }
        )
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {

        var culL = paddingLeft
        var culT = paddingTop
        for ( i in 0 until allLines.size){
            val line = allLines.get(i)
            val lineHeight = lineHeights.get(i)
            for (j in 0 until line.size) {
              val view =   line.get(j)
                val left = culL
                val top =culT

                //view. getMeasuredWidth是测量之后就能拿到，view. getWidth是layout之后才能获取
                val width = view.measuredWidth
                val height = view.measuredHeight
                 val right = width+left
                val bottom = height+top

                view.layout(left,top,right,bottom)
                culL = (right+lineHorSpec).toInt()//一个view完事之后加上横向间距
                System.out.println("$i===行====$culL")
            }
            culT += (lineHeight+lineHorSpec).toInt()
            System.out.println("$i===行====$culT")
            culL = paddingLeft

        }

    }
}